package cn.jiyun;

import cn.jiyun.mapper.UserMapper;
import cn.jiyun.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

/**
 * @author Fay
 * @date 2022/6/22&11:19
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
    @Autowired
    private UserMapper userMapper;
    /**
     *根据id查询
     */
    @Test
    public void findById(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }
    /**
     * 添加
     */
    @Test
    public void findAdd(){
        User user = new User();
        user.setPassword("123");
        user.setUsername("xiaohong");
        user.setAge(15);
        user.setEmail("test6@itcast.cn");
        user.setNickName("张三");
        userMapper.insert(user);
    }
    /**
     * 修改
     */
    @Test
    public void findUpdate(){
        User user = new User();
        user.setPassword("123");
        user.setUsername("1");
        user.setAge(15);
        user.setEmail("test6@itcast.cn");
        user.setNickName("1");
        user.setId(5L);
        userMapper.updateById(user);
    }
    /**
     * 删除
     */
    @Test
    public void findDelete(){
        userMapper.deleteById(5L);
    }
    /**
     * 查询
     */
    @Test
    public void findCondition(){
        //设置查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //age 大于10
        queryWrapper.gt("age","10");
        //username包含zh
        queryWrapper.like("username","zh");
        List<User> userList = userMapper.selectList(queryWrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }
    /**
     * 自定义查询
     */
    @Test
    public void selectCountGroupByAge(){
        List<Map> list = userMapper.selectCountGroupByAge();
        System.out.println(list);
    }
    /**
     * 分页查询
     */
    @Test
    public void findPage(){
        Page<User> page = new Page<>(2,3);
        Page<User> userPage = userMapper.selectPage(page, null);
        System.out.println("总记录数"+userPage.getTotal());
        System.out.println("总条数"+userPage.getSize());
        List<User> userList = userPage.getRecords();
        System.out.println(userList);
    }
}
